package com.cqut.controller;


import com.cqut.VO.course.CourseDetailsVO;
import com.cqut.VO.course.CourseListDisplayVO;
import com.cqut.entity.UserCourse;
import com.cqut.VO.courseMemberInfo.StudentInfoVO;
import com.cqut.VO.courseMemberInfo.TeacherInfoVO;
import com.cqut.entity.Course;
import com.cqut.service.CourseService;
import com.cqut.util.ResultVOUtil;
import com.cqut.VO.course.CourseListVO;
import com.cqut.VO.result.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 冯永龙
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @GetMapping("/main/{userId}/{type}")
    public ResultVO<List<CourseListVO>> getCourse(@PathVariable("userId") Integer userId, @PathVariable("type") Integer type) {
        Map<String, List<CourseListDisplayVO>> map = courseService.getCourses(userId, type);
        if (map != null) {
            return ResultVOUtil.success(map);
        }
        return ResultVOUtil.fail();
    }

    @DeleteMapping("/delete/{courseId}")
    public ResultVO<Object> deleteCourse(@PathVariable("courseId") Integer courseId) {
        if (courseService.deleteCourseById(courseId)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @PutMapping("/update")
    public ResultVO<Object> updateCourse(@RequestBody Course course) {
        if (courseService.updateCourse(course)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/insert/{userId}/{sort}")
    public ResultVO<Object> creatCourse(@RequestBody Course course, @PathVariable("userId") Integer userId, @PathVariable("sort") Integer sort) {
        if (courseService.createCourse(course, userId, sort)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @PutMapping("/add/{courseCode}/{userId}/{sort}")
    public ResultVO<Object> addCourse(@PathVariable("courseCode") String courseCode,
                                      @PathVariable("userId") Integer userId,
                                      @PathVariable("sort") Integer sort) {
        if (courseService.addCourse(courseCode, userId, sort)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/getTeacherList/{courseId}")
    public ResultVO<List<TeacherInfoVO>> getCourseTeacherInfo(@PathVariable("courseId") Integer courseId) {
        List<TeacherInfoVO> teachTeamList = courseService.getTeachTeamList(courseId);
        if (teachTeamList != null) {
            return ResultVOUtil.success(teachTeamList);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/getStudentList/{courseId}")
    public ResultVO<List<StudentInfoVO>> getCourseStudentList(@PathVariable("courseId") Integer courseId) {
        List<StudentInfoVO> studentList = courseService.getStudentList(courseId);
        if (studentList != null) {
            return ResultVOUtil.success(studentList);
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/updateRankCourse")
    public ResultVO<Object> updateRankCourse(@RequestBody List<UserCourse> courses) {
        if (courseService.updateRankCourse(courses)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/getCourseDetails/{courseId}")
    public ResultVO<CourseDetailsVO> getCourseDetails(@PathVariable("courseId") Integer courseId) {
        CourseDetailsVO courseDetails = courseService.getCourseDetails(courseId);
        if (courseDetails != null) {
            return ResultVOUtil.success(courseDetails);
        }
        return ResultVOUtil.fail();
    }

    @DeleteMapping("/quitCourse/{courseId}/{userId}")
    public ResultVO<Object> quitCourse(@PathVariable("courseId") Integer courseId, @PathVariable("userId") Integer userId) {
        if (courseService.quitCourse(courseId, userId)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }
}

